/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once


#include <kmhdfs/client/token.h>
#include <kmhdfs/server/datanode_info.h>
#include <kmhdfs/server/extended_block.h>

#include <vector>

namespace Hdfs {
    namespace Internal {
        /**
         * Transfer data to/from datanode using a streaming protocol.
         */
        class DataTransferProtocol {
        public:
            virtual ~DataTransferProtocol() {
            }

            /**
             * Read a block.
             *
             * @param blk the block being read.
             * @param blockToken security token for accessing the block.
             * @param clientName client's name.
             * @param blockOffset offset of the block.
             * @param length maximum number of bytes for this read.
             */
            virtual void readBlock(const ExtendedBlock &blk,
                                   const Token &blockToken, const char *clientName,
                                   int64_t blockOffset, int64_t length) = 0;

            /**
             * Write a block to a datanode pipeline.
             *
             * @param blk the block being written.
             * @param blockToken security token for accessing the block.
             * @param clientName client's name.
             * @param targets target datanodes in the pipeline.
             * @param source source datanode.
             * @param stage pipeline stage.
             * @param pipelineSize the size of the pipeline.
             * @param minBytesRcvd minimum number of bytes received.
             * @param maxBytesRcvd maximum number of bytes received.
             * @param latestGenerationStamp the latest generation stamp of the block.
             */
            virtual void writeBlock(const ExtendedBlock &blk,
                                    const Token &blockToken, const char *clientName,
                                    const std::vector<DatanodeInfo> &targets, int stage,
                                    int pipelineSize, int64_t minBytesRcvd, int64_t maxBytesRcvd,
                                    int64_t latestGenerationStamp, int checksumType,
                                    int bytesPerChecksum) = 0;

            /**
             * Transfer a block to another datanode.
             * The block stage must be
             * either {@link BlockConstructionStage#TRANSFER_RBW}
             * or {@link BlockConstructionStage#TRANSFER_FINALIZED}.
             *
             * @param blk the block being transferred.
             * @param blockToken security token for accessing the block.
             * @param clientName client's name.
             * @param targets target datanodes.
             */
            virtual void transferBlock(const ExtendedBlock &blk,
                                       const Token &blockToken, const char *clientName,
                                       const std::vector<DatanodeInfo> &targets) = 0;

            /**
             * Get block checksum (MD5 of CRC32).
             *
             * @param blk a block.
             * @param blockToken security token for accessing the block.
             * @throw HdfsIOException
             */
            virtual void blockChecksum(const ExtendedBlock &blk,
                                       const Token &blockToken) = 0;

            /**
             * Request short circuit access file descriptors from a DataNode.
             *
             * @param blk             The block to get file descriptors for.
             * @param blockToken      Security token for accessing the block.
             * @param clientName      client's name.
             * @param maxVersion      Maximum version of the block data the client
             *                          can understand.
             */
            virtual void requestShortCircuitFds(const ExtendedBlock blk,
                                                const Token &blockToken,
                                                uint32_t maxVersion) = 0;
        };
    }
}

